<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-11-8
 * Time: 下午4:50
 */

namespace App\Http\Controllers\Home;

use App\Exceptions\MyException;
use Illuminate\Http\Request;
use App\Repositories;
use Illuminate\Support\Facades\DB;

class DocumentController extends BaseController
{
    /**
     * 获取当前控制器服务仓库对象
     * @return mixed
     */
    public function getServiceRepository() : Repositories\Portal\DocumentRepository
    {
        return Repositories\Portal\DocumentRepository::getInstance();
    }

    /**
     * 资讯列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function newsList(Request $request)
    {
        return $this->withData('contentClass', 'bg-7')->withData('newsTopLeftList', Repositories\Basic\AdvRepository::getInstance()->getAdvDataListByName('news_top_left_list'))->withData('newsTopRightList', Repositories\Basic\AdvRepository::getInstance()->getAdvDataListByName('news_top_right_list'))->lists($request);
    }

    /**
     * 图集列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function photoList(Request $request)
    {
        return $this->withData('contentClass', 'bg-7')->lists($request);
    }

    /**
     * 单网页列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function pageList(Request $request)
    {
        return $this->withData('contentClass', 'bg-7')->lists($request);
    }

    /**
     * 栏目列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function lists(Request $request)
    {
        $oneCateId = $request->route('one_cate_id');   //  一级栏目编号

        $twoCateId = $request->route('two_cate_id'); //  二级栏目编号

        //  子栏目列表
        $childCategoryList = Repositories\Portal\CategoryRepository::getInstance()->getCategoryList($oneCateId);

        $categoryIds = [];
        if (empty($twoCateId)) {
            $categoryDetail = Repositories\Portal\CategoryRepository::getInstance()->getDetailById($oneCateId);
            $pCategoryDetail = $categoryDetail;
            if (empty($childCategoryList->toArray())) {
                $categoryIds = $oneCateId;
            } else {
                foreach ($childCategoryList as $key=>$value) {
                    $categoryIds[$key] = $value->id;
                }
            }
        } else {
            $categoryDetail = Repositories\Portal\CategoryRepository::getInstance()->getDetailById($twoCateId);
            $pCategoryDetail = Repositories\Portal\CategoryRepository::getInstance()->getDetailById($oneCateId);
            $categoryIds = $twoCateId;
        }

        //  文档类型
        $documentType = $categoryDetail->document_type;

        $model = $this->getServiceRepository()->getSelectModel([
            'document_type' => $documentType,
            'category_id' => $categoryIds,
            'status' => 99,
        ]);

        if ($pCategoryDetail->id == 6) {
            $paginateData = $model->orderBy('id', 'asc')->paginate(get_value($categoryDetail, 'page_limit', config('home.config.page_size', 12)));
        } else {
            $paginateData = $model->orderBy('fixed_release_time', 'desc')->paginate(get_value($categoryDetail, 'page_limit', config('home.config.page_size', 12)));
        }


        $template = get_value($categoryDetail, 'template_list', 'document.list.'.$documentType);

        //  ajax 提交
        if ($request->ajax()) {

            $content = view('home.'.$template.'_list', ['data'=>$paginateData]);

            $content = response($content)->getContent();

            return $this->success('列表获取成功', '', ['content'=>$content, 'current_page'=>$paginateData->currentPage(), 'last_page'=>$paginateData->lastPage()]);
        }

        $this->getSeoInfo($categoryDetail, $pCategoryDetail, []);

        return $this->fetch($template, [
            'template' => $template,
            'categoryDetail' => $categoryDetail,    //  当前栏目信息
            'data' => $paginateData,        //  文档分页信息
            'pCategoryDetail' => $pCategoryDetail,  //  父级栏目信息
            'childCategoryList' => $childCategoryList,  //  栏目列表
            'oneCateId' => $oneCateId,
            'twoCateId' => $twoCateId,
            'currentUrl' => get_home_url($documentType.'/list/'.$oneCateId.'/'.$twoCateId)
        ]);
    }

    /**
     * 资讯详情
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function newsDetail(Request $request)
    {
        return $this->detail($request);
    }

    /**
     * 图集详情
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function photoDetail(Request $request)
    {
        return $this->detail($request);
    }

    /**
     * 单网页详情
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function pageDetail(Request $request)
    {
        return $this->detail($request);
    }

    /**
     * 获取详情页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function detail(Request $request)
    {
        $id = $request->route('id');

        $detail = $this->getServiceRepository()->getDetailById($id);

        $documentType = $detail->document_type;

        $categoryDetail = Repositories\Portal\CategoryRepository::getInstance()->getDetailById($detail->category_id);

        $pCategoryDetail = Repositories\Portal\CategoryRepository::getInstance()->getDetailById($categoryDetail->pid);

        if ($detail->{$documentType}->template_detail) {
            $template = $detail->{$documentType}->template_detail;
        } else if ($categoryDetail->template_detail) {
            $template = $categoryDetail->template_detail;
        } else if (get_value($pCategoryDetail, 'template_detail', '')) {
            $template = $pCategoryDetail->template_detail;
        } else {
            $template = 'document.detail.'.$documentType;
        }

        $childCategoryList = [];
        if ($documentType == 'photo' && !empty($pCategoryDetail->id)) {
            $childCategoryList = Repositories\Portal\CategoryRepository::getInstance()->getCategoryList($pCategoryDetail->id);
        }

        $this->getSeoInfo($categoryDetail, $pCategoryDetail, $detail);

        $photoList = get_value($detail->$documentType, 'pic_text' , []);

        $photoList = empty($photoList) ? [] : json_decode($photoList);

//        DB::table('portal_document')->increment('click');

        // 上一篇
        $prevDetail = $this->getServiceRepository()->getPrevDetailById($id, $detail->category_id);
        // 下一篇
        $nextDetail = $this->getServiceRepository()->getNextDetailById($id, $detail->category_id);

        return $this->fetch($template, ['detail'=>$detail, 'documentType'=>$documentType, 'categoryDetail'=>$categoryDetail, 'pCategoryDetail'=>$pCategoryDetail, 'childCategoryList'=>$childCategoryList, 'photoList'=>$photoList, 'prevDetail'=>$prevDetail, 'nextDetail'=>$nextDetail]);
    }


    /**
     * 搜索页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function search(Request $request)
    {
        $keywords = $request->input('keywords', '');
        $data = $this->getServiceRepository()->getSelectModel([
            'keywords' => $keywords,
        ])->orderBy('id', 'desc')->paginate(20);

        $seo = ['title'=>$keywords.' 搜索 - '.config('seo_title', '统景温泉'), 'keywords'=>config('seo_keywords', '统景温泉'), 'description'=>config('seo_description', '统景温泉')];

        return $this->fetch('document.search', ['seo'=>$seo, 'data'=>$data, 'keywords'=>$keywords]);
    }
}